package com.luo.dao.impl;

import com.luo.dao.UserMappers;
import com.luo.model.GetUserInfoVO;
import com.luo.model.User;
import com.luo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/1/25 11:42
 */
public class UserDaoImpl implements UserMappers {
    @Override
    public User queryUserByUserNameAndPassWord(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        User userByUserNameAndPassWord = mapper.queryUserByUserNameAndPassWord(user);
        sqlSession.commit();
        sqlSession.close();
        return userByUserNameAndPassWord;
    }

    @Override
    public User queryUserById(final String id) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        User user = mapper.queryUserById(id);
        sqlSession.commit();
        sqlSession.close();
        return user;
    }

    @Override
    public int adminChangePassword(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        int i = mapper.adminChangePassword(user);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int updateAdmin(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        int i = mapper.updateAdmin(user);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int updateAdminPicture(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        int i = mapper.updateAdminPicture(user);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public User queryUserByUserName(String username) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        User user = mapper.queryUserByUserName(username);
        sqlSession.close();
        return user;
    }

    public List<GetUserInfoVO> getUserInfo() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        List<GetUserInfoVO> userList = mapper.getUserInfo();
        sqlSession.close();
        return userList;
    }

    @Override
    public int deleteUserByIds(List list) {
        SqlSession sqlSession = null;
        int rows = 0;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMappers mapper = sqlSession.getMapper(UserMappers.class);
            rows = mapper.deleteUserByIds(list);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return rows;
    }

    @Override
    public int updateUserInfo(GetUserInfoVO user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        int row = mapper.updateUserInfo(user);
        sqlSession.commit();
        sqlSession.close();
        return row;
    }

    @Override
    public User queryUserByPhonne(String phone) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMappers mapper = sqlSession.getMapper(UserMappers.class);
        User user = mapper.queryUserByPhonne(phone);
        sqlSession.commit();
        sqlSession.close();
        return user;
    }
}
